package com.x.provider.customer.factory.dto.impl;

import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.model.dto.customer.CustomerDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerResourceDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerListReqDTO;
import com.x.provider.customer.factory.dto.CustomerDTOBuilder;
import com.x.provider.customer.model.domain.customer.CustomerResource;
import com.x.provider.customer.service.customer.CustomerResourceService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: liushenyi
 * @date: 2022/07/25/16:04
 */
@Service("customerResourceBuilder")
public class CustomerResourceBuilderImpl implements CustomerDTOBuilder {

   private final CustomerResourceService customerResourceService;

   public CustomerResourceBuilderImpl(CustomerResourceService customerResourceService){
      this.customerResourceService = customerResourceService;
   }

   @Override
   public void build(CustomerListReqDTO listCustomerAO, Map<Long, CustomerDTO> dest) {
      if (!listCustomerAO.getCustomerOptions().contains(CustomerOptions.CUSTOMER_RESOURCE.name())){
         return;
      }
      Map<Long, List<CustomerResource>> customerResourceMap = customerResourceService.listCustomerResource(listCustomerAO.getCustomerIds());
      customerResourceMap.entrySet().stream().forEach(item -> {
         dest.get(item.getKey()).setCustomerResourceList(BeanUtil.prepare(item.getValue(), CustomerResourceDTO.class));
      });
   }
}
